package com.ws.shop.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.stereotype.Repository;

import com.ws.shop.bean.Page;
import com.ws.shop.dao.PaymentDao;
import com.ws.shop.entity.Payment;

@Repository(value="paymentDao")
public class PaymentDaoImpl extends BaseDaoImpl implements PaymentDao {

	final String findPaymentListPage_count = "select count(1) from jvf_payment";
	final String findPaymentListPage_list = "select id,interface,business,mail,seckey,currency,fee,state from jvf_payment";
	final String findPaymentListPage_order = " order by id";
	public Page findPaymentListPage(int pageno,int pagesize) {
		return findPage(pageno,pagesize,findPaymentListPage_count
				,new StringBuilder(findPaymentListPage_list).append(findPaymentListPage_order));
	}
	
	final String save_sql = "insert into jvf_payment(interface,business,mail,seckey,currency,fee,state) values(?,?,?,?,?,?,?)";
	final String update_sql = "update jvf_payment set interface=?,business=?,mail=?,seckey=?,currency=?,fee=?,state=? where id=?";
	public void saveOrUpdate(Payment payment) {
		if(payment.getId() == 0){
			this.update(save_sql, payment.getInterfaces(),payment.getBusiness(),payment.getMail(),payment.getSeckey(),payment.getCurrency(),payment.getFee(),payment.getState());
		}else{
			this.update(update_sql, payment.getInterfaces(),payment.getBusiness(),payment.getMail(),payment.getSeckey(),payment.getCurrency(),payment.getFee(),payment.getState(),payment.getId());
		}
	}

	public Payment getPaymentByid(final int id ){
		String s = "select id,interface,business,mail,seckey,currency,fee,state from jvf_payment where id = " + id;
		return this.query(s, new ResultSetExtractor<Payment>() {
			public Payment extractData(ResultSet rs) throws SQLException,
					DataAccessException {
				Payment payment = null;
				while (rs.next()) {
					payment = new Payment();
				    payment.setId(rs.getInt(1));
				    payment.setInterfaces(rs.getString(2));
				    payment.setBusiness(rs.getString(3));
				    payment.setMail(rs.getString(4));
				    payment.setSeckey(rs.getString(5));
				    payment.setCurrency(rs.getInt(6));
				    payment.setFee(rs.getInt(7));
				    payment.setState(rs.getInt(8));
				}
				return payment;
			}
		});
	}
	
}
